package com.tl.satoken.domain.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 专门用于Redis存储的Player DTO类
 * 排除掉不能序列化的字段，如ChannelHandlerContext
 */
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RedisPlayer implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private Long id;
    private Integer heroId;
    private String username;
    private Rotation rotation;
    private String animation;
    private String status;
    private Position position;
    private String roomId;
    private String sessionId;
    
    /**
     * 从Player对象转换为RedisPlayer
     */
    public static RedisPlayer fromPlayer(Player player) {
        if (player == null) {
            return null;
        }
        return RedisPlayer.builder()
                .id(player.getId())
                .heroId(player.getHeroId())
                .username(player.getUsername())
                .rotation(player.getRotation())
                .animation(player.getAnimation())
                .status(player.getStatus())
                .position(player.getPosition())
                .roomId(player.getRoomId())
                .sessionId(player.getSessionId())
                .build();
    }
    
    /**
     * 转换为Player对象
     * 注意：转换后的Player对象不包含channel信息
     */
    public Player toPlayer() {
        return Player.builder()
                .id(this.id)
                .heroId(this.heroId)
                .username(this.username)
                .rotation(this.rotation)
                .animation(this.animation)
                .status(this.status)
                .position(this.position)
                .roomId(this.roomId)
                .sessionId(this.sessionId)
                .channel(null) // channel信息不存储在Redis中
                .build();
    }
}
